﻿package  
{	
	import ben.SomeTools;
	import com.greensock.TweenMax;
	import fausto.ateliers.AteliersContent;
	import fausto.bio.BioContent;
	import fausto.contact.ContactContent;
	import fausto.cycles.CyclesContent;
	import fausto.Menu;
	import fausto.news.NewsContent;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.text.TextField;
	/**
	* ...
	* @author Ben
	*/
	public class Main extends Sprite
	{							
		public var _menu:Menu
		public var _menuArea:Sprite
		public var _menuBg:Sprite
		public var _logo:Sprite
		
		public var _titleTxt:TextField
		
		// PARTS
		public var _newsContent:NewsContent
		public var _cyclesContent:CyclesContent
		public var _ateliersContent:AteliersContent
		public var _bioContent:BioContent
		public var _contactContent:ContactContent
		
		private var _i:Infos
		
		public function Main() 
		{
			_i = Infos.getInstance();
			addEventListener(Event.ADDED_TO_STAGE, onStaged);
			visible = false;
		}
		
		private function onStaged(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onStaged);
			stage.align = StageAlign.TOP;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.addEventListener(Event.RESIZE, onResized);
			onResized();
		}
		
		public function open(e:Event = null):void 
		{			
			visible = true;
			_menuArea.addEventListener(Event.ENTER_FRAME, openCloseMenu);
			_menuArea.mouseEnabled = false;
			_menu.createMenu();
			
			_newsContent.createNews();
			_cyclesContent.createItems();
			_bioContent.createBio();
			_contactContent.createContact();
			
			managePart(null, 1);
			
			addEventListener("MENU_CLICK", managePart);
			
			onResized();
		}		
		
		private var currentPart:int = -1
		
		private function managePart(e:Event = null, _id:int = -1):void 
		{
			closeCurrent();
			
			var n:int = (_id == -1) ? e.target._id : _id;
			
			_titleTxt.text = "./ " +_i.theXML.part[n].label;
			
			switch(n)
			{
				case 0:
					_newsContent.visible = true;
					break;
				case 1:
					_cyclesContent.visible = true;
					break;
				case 2:
					_ateliersContent.visible = true;
					break;
				case 3:
					_bioContent.visible = true;
					break;
				case 4:
					_contactContent.visible = true;
					break;
			}
			
			currentPart = n;
		}
		
		private function closeCurrent():void 
		{
			switch(currentPart)
			{
				case 0:
					_newsContent.visible = false;
					// GO LEFT
					break;
				case 1:
					_cyclesContent.visible = false;
					// GO LEFT
					break;
				case 2:
					_ateliersContent.visible = false;
					break;
				case 3:
					_bioContent.visible = false;
					break;
				case 4:
					_contactContent.visible = false;
					break;
			}
		}
		
		private function openCloseMenu(e:Event):void 
		{
			if (_menuArea.hitTestPoint(mouseX, mouseY))
			{
				if (!_menu._open)
				{
					_menu.openMenu();
					TweenMax.to(_menuBg, 0.4, { y:stage.stageHeight - 50 } );
					TweenMax.to(_logo, 0.4, { y:19 } );
				}
				
			}
			else
			{
				if (_menu._open)
				{
					_menu.closeMenu();
					TweenMax.to(_menuBg, 0.4, { delay:0.2, y:stage.stageHeight - 5 } );
					TweenMax.to(_logo, 0.4, { delay:0.2, y:-73 } );
				}
			}
		}
		
		private function onResized(e:Event = null):void 
		{
			if (_menu._open)
			{
				_menu.y = _menuBg.y = stage.stageHeight - 50;
				_logo.y = 19;
				
			}
			else
			{
				_menu.y = _menuBg.y = stage.stageHeight - 5;
				_logo.y = -73;
			}
			
			_menuArea.width = _menuBg.width = stage.stageWidth;
			_menuArea.x = _menuBg.x = -Math.round((stage.stageWidth - _i.sceneWidth) / 2);
			_menuArea.y = stage.stageHeight - 70;
			_menu.x = Math.round(_i.sceneWidth - _menu.width) / 2;
		}
	}
	
}
